import { AssetManager, AudioClip, Material } from "cc";
import { PREVIEW } from "cc/env";
import Core from "db://app/Core";
import AudioEngine from "db://app/manager/sound/AudioEngine";
import { i18n, IAssetBundle, IDevice } from "db://fairygui-xforge/UIDevice";

export class FGUIDeviceImpl implements IDevice {
  i18n: i18n = null;
  graphicsMaterial: Material;

  playOneShotSound (clip: AudioClip, volumeScale?: number): void {
    AudioEngine.inst.playEffect(clip, volumeScale, false);
  }

  loadBundle (bundleName: string): Promise<IAssetBundle> {
    return new Promise<IAssetBundle>((resolve, reject) => {
      Core.inst.manager.loader.loadBundle({
        bundle: bundleName,
        onComplete: (result: AssetManager.Bundle) => {
          if (result) {
            const ab = Core.inst.manager.loader.getAssetsBundle(bundleName);
            if (PREVIEW) {
              ab.addRef();
            }
            resolve(ab);
          } else {
            reject("no bundle loaded");
          }
        }
      });
    });
  }
}